#!/bin/sh

echo "Starting ..."

echo ">> Deleting old migrations ..."
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete
echo ">> Old migrations deleted successfully!"

echo ">> Deleting database ..."
find . -name "db.sqlite3" -delete
echo ">> Database deleted successfully!"

echo ">> Deleting uploads ..."
rm -rf ./uploads
echo ">> Uploads deleted successfully!"

echo ">> Running python manage.py makemigrations ..."
python manage.py makemigrations
echo ">> python manage.py makemigrations runned successfully!"

echo ">> Running python manage.py migrate ..."
python manage.py migrate
echo ">> python manage.py migrate ran successfully!"

echo "\
print ''
print '>> Creating superuser ...';\
from django.contrib.auth.models import User;\
User.objects.filter(email='admin@example.com').delete();\
User.objects.create_superuser('admin', 'admin@example.com', 'adminadmin');\
print '>> Superuser created successfully!';\
print '>> Initializing data ...';\
import init;\
print '>> Data initialized successfully!';\
" | python manage.py shell

echo ""
echo ">> Done"

echo ">> Running python manage.py crontab remove ..."
python manage.py crontab remove
echo ">> python manage.py crontab remove ran successfully!"

echo ">> Running python manage.py crontab add ..."
#python manage.py crontab add
echo ">> python manage.py crontab add ran successfully!"

echo ">> Running python manage.py runserver ..."
python manage.py runserver